package com.tianxin.workbao.workBao.Activity;

import android.content.Context;
import android.os.Bundle;
import androidx.annotation.Nullable;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.tabs.TabLayout;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.widget.Toolbar;
import android.util.AttributeSet;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;

import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.adapter.FragmentStareAdapter;
import com.tianxin.workbao.workBao.fragment.CoalStatisticFragment;

/**
 * Created by hacker on 2017/7/18.
 */

public class CoalStatisticActivity extends BaseActivity implements View.OnClickListener {
    private FloatingActionButton mRefreshFab;
    private ViewPager mViewPager;
    private TabLayout mTabLayout;
    private FragmentStareAdapter mAdapter;
    private TextView pageTitleTV;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.common_main_tablayout);
        super.initStatusBar(false);
        initView();
        initEvent();
        initData();
        bindView();
    }


    @Override
    public View onCreateView(String name, Context context, AttributeSet attrs) {
        return super.onCreateView(name, context, attrs);
    }

    private void initView() {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mToolbar.setTitle("");
        mToolbar.setNavigationIcon(R.drawable.ic_action_back_24dp);
        pageTitleTV = (TextView) findViewById(R.id.toolbar_title);
        mRefreshFab = (FloatingActionButton) findViewById(R.id.top_fab);

        mViewPager = (ViewPager) findViewById(R.id.viewPager);
        mTabLayout = (TabLayout) findViewById(R.id.tabLayout);

        mViewPager.setCurrentItem(0);
        mViewPager.setOffscreenPageLimit(3);
        mViewPager.setAdapter(mAdapter);
        mTabLayout = (TabLayout) findViewById(R.id.tabLayout);
        mTabLayout.setupWithViewPager(mViewPager);
        mTabLayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.write));
    }

    @Override
    public void initData() {
        mTabTitles.add(0, "客户供应对比");
        mTabTitles.add(1, "客户采购对比");
        mTabTitles.add(2, "煤种采购对比");
        mTabTitles.add(3, "煤种销售对比");

        mFragments.add(CoalStatisticFragment.newInstance(0));
        mFragments.add(CoalStatisticFragment.newInstance(1));
        mFragments.add(CoalStatisticFragment.newInstance(2));
        mFragments.add(CoalStatisticFragment.newInstance(3));

        mAdapter = new FragmentStareAdapter(getSupportFragmentManager(), mFragments, mTabTitles);
    }

    @Override
    public void initEvent() {
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CoalStatisticActivity.this.finish();
            }
        });
        mRefreshFab.setOnClickListener(this);
    }

    @Override
    public void bindView() {
        pageTitleTV.setText("统计");
        mViewPager.setAdapter(mAdapter);
    }

    //View.OnClickListener
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.top_fab:
                updateUi();
                break;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.add_share_menu, menu);
        return true;
    }

    private void updateUi() {
        mFragments.get(mTabLayout.getSelectedTabPosition()).initData(false);
    }
}
